const fs = require("fs");
const path = require("path");

// 生成文件树
function generateTree(dirPath) {
  // TODO：待补充代码
  const files = fs.readdirSync(dirPath);
  const tree = [];
  console.log(files);
  files.forEach((file) => {
    const filePath = path.join(dirPath, file);
    const isDirectory = fs.statSync(filePath).isDirectory();
    if (isDirectory) {
      const subtree = generateTree(filePath);
      tree.push({ name: file, children: subtree });
    } else {
      tree.push({ name: file });
    }
  });
  return tree;
}

// 根据数据生成 tree 命令
function generateDirectoryStructure(data, indent = "") {
  let result = "";
  for (let i = 0; i < data.length; i++) {
    const item = data[i];
    const name = item.name;
    const isLast = i === data.length - 1;

    result += `${indent}${isLast ? "└──" : "├──"} ${name}\n`;

    if (item.children) {
      const childIndent = indent + (isLast ? "    " : "│   ");
      result += generateDirectoryStructure(item.children, childIndent);
    }
  }

  return result;
}

// 读取指定文件夹
const directoryPath = __dirname;
const tree = generateTree(directoryPath);
console.log(tree);
const treem = generateDirectoryStructure(tree);
console.log(treem);

// 以下代码检测需要，请勿删除
module.exports = generateTree;
